/*
 * Copyright 2009 Eat Local Food, LLC
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client;

import java.util.Iterator;

import com.elf.gwt.admin.client.components.AccountComponent;
import com.elf.gwt.admin.client.components.AccountsGroupComponent;
import com.elf.gwt.admin.client.components.AdminSimpleComponent;
import com.elf.gwt.admin.client.components.CatalogGroupComponent;
import com.elf.gwt.admin.client.components.ConfigurationGroupComponent;
import com.elf.gwt.admin.client.components.CurrenciesAdministrationComponent;
import com.elf.gwt.admin.client.components.GroupComponent;
import com.elf.gwt.admin.client.components.GroupMemberComponent;
import com.elf.gwt.admin.client.components.JSONServerComponent;
import com.elf.gwt.admin.client.components.LanguagesAdministrationComponent;
import com.elf.gwt.admin.client.components.LocalizationGroupComponent;
import com.elf.gwt.admin.client.components.LocationGroupComponent;
import com.elf.gwt.admin.client.components.ManufacturersAdministrativeComponent;
import com.elf.gwt.admin.client.components.ModuleGroupComponent;
import com.elf.gwt.admin.client.components.MultipleResultsComponent;
import com.elf.gwt.admin.client.components.ReportsGroupComponent;
import com.elf.gwt.admin.client.components.ToolsGroupComponent;
import com.elf.gwt.common.client.CommonComponentController;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.components.CommonSimpleComponent;
import com.elf.gwt.common.client.model.AbstractModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AdminComponentController extends CommonComponentController
{	
	protected GWTCommerceConstants adminConstants;

	protected String storeNameAddress = "";

	protected HorizontalPanel horizontalPanel;

	public static CurrenciesAdministrationComponent currenciesAdministrationComponent = null;
	public static LanguagesAdministrationComponent languagesAdministrationComponent = null;
	public static ManufacturersAdministrativeComponent manufacturersAdministrativeComponent = null;
	public static AccountComponent accountComponent = null;

	protected NavigationStackPanel navigationStackPanel;

	public class NavigationStackPanel extends StackPanel
	{
		public NavigationStackPanel()
		{
			super();
		}

		public void onBrowserEvent(Event event)
		{
			if (!JSONServerComponent.inuse) {
				super.onBrowserEvent(event);
				int index = this.getSelectedIndex();
				GroupComponent groupComponent = (GroupComponent)this.getWidget(index);
				int loc = groupComponent.getHandle().indexOf("Group");
				int selectedId = groupComponent.getSelectedGroupModel().getId();
				String history = groupComponent.getHandle().substring(0, loc) + "Component--" + selectedId;
				groupComponent.onSelection(history);
			}
		}
	}

	public Panel getDisplayPanel() {
		return horizontalPanel;
	}


	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{
		adminConstants = GWT.create(GWTCommerceConstants.class);
		multipleResultsComponent = new MultipleResultsComponent(this);

		super.onModuleLoad();
	}

	protected void processOnModuleLoadBuildDisplayFramework()
	{
		horizontalPanel = new HorizontalPanel();
		navigationStackPanel = new NavigationStackPanel();
		horizontalPanel.add(navigationStackPanel);
	}

	protected String processOnModuleLoadParseMetaData(JSONValue jsonInitialValue)
	{
		String token = "";

		JSONValue metaDataValue = CommonUtils.getComponentValue(jsonInitialValue, "GWTCommerceAdminMetaData");

		if (metaDataValue != null)
		{
			JSONObject jsonObject;
			if ((jsonObject = metaDataValue.isObject()) != null)
			{
				token = AbstractModel.convertToString(jsonObject,"token");
				boolean testSSL = AbstractModel.convertToBoolean(jsonObject, "testSSL");
				storeNameAddress = AbstractModel.convertToString(jsonObject, "store_name_address");
				if(!GWT.isScript())
				{
					CommonUtils.testSSL = testSSL;
				}
			}
		}

		return token;
	}

	protected void processOnModuleLoadParseDataPre()
	{
		languagesAdministrationComponent = (LanguagesAdministrationComponent)getComponent("LanguagesAdministrationComponent");
		currenciesAdministrationComponent = (CurrenciesAdministrationComponent)getComponent("CurrenciesAdministrationComponent");
		manufacturersAdministrativeComponent = (ManufacturersAdministrativeComponent)getComponent("ManufacturersAdministrativeComponent");
		accountComponent = (AccountComponent)getComponent("AccountComponent");
	}

	protected void processOnModuleLoadDefaultDisplay()
	{
		if (!accountComponent.getAccountMetaModel().isRequireAdministrator() || accountComponent.getAccountModel().getId() > 0) {
			History.newItem("AccountsComponent--3");
		}
	}


	public void hideComponents()
	{
		for (Iterator<String> iter = componentHash.keySet().iterator(); iter.hasNext(); )
		{
			String handle = iter.next();
			if (!handle.endsWith("GroupComponent"))
			{
				CommonSimpleComponent component = componentHash.get(handle);
				component.setVisible(false);
			}
		}
	}

	public String getComponentTitle(String handle)
	{
		String title = handle;
		if (handle == null)
		{
		}
		else if (handle.equals("AccountsGroupComponent")) title = adminConstants.AccountGroupComponentTitle();
		else if (handle.equals("ConfigurationGroupComponent")) title = adminConstants.ConfigurationGroupComponentTitle();
		else if (handle.equals("CatalogGroupComponent")) title = adminConstants.CatalogGroupComponentTitle();
		else if (handle.equals("ModuleGroupComponent")) title = adminConstants.ModuleGroupComponentTitle();
		else if (handle.equals("LocationGroupComponent")) title = adminConstants.LocationGroupComponentTitle();
		else if (handle.equals("LocalizationGroupComponent")) title = adminConstants.LocalizationGroupComponentTitle();
		else if (handle.equals("ReportsGroupComponent")) title = adminConstants.ReportsGroupComponentTitle();
		else if (handle.equals("ToolsGroupComponent")) title = adminConstants.ToolsGroupComponentTitle();

		return title;
	}

	protected void updateFromHistory(String historyToken)
	{
		hideComponents();
		super.updateFromHistory(historyToken);
	}

	public CommonSimpleComponent getComponent(String handle, boolean create)
	{
		if (handle.endsWith("ComponentMetaData")) return null;

		CommonSimpleComponent simpleComponent = componentHash.get(handle);
		Widget component = null;
		if (simpleComponent == null && create)
		{
			if (handle.equals("dummy")) component = null;

			else if (handle.equals("AccountComponent")) {
				component = new AccountComponent(this, null);
				getDisplayPanel().add(component);
				setComponent((CommonSimpleComponent)component);
			}

			else if (handle.equals("AccountsGroupComponent")) {
				component = new AccountsGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}
			else if (handle.equals("ConfigurationGroupComponent")) {
				component = new ConfigurationGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}
			else if (handle.equals("CatalogGroupComponent")) {
				component = new CatalogGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}
			else if (handle.equals("LocalizationGroupComponent")) {
				component = new LocalizationGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}
			else if (handle.equals("LocationGroupComponent")) {
				component = new LocationGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}
			else if (handle.equals("ModuleGroupComponent")) {
				component = new ModuleGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}
			else if (handle.equals("ReportsGroupComponent")) {
				component = new ReportsGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}
			else if (handle.equals("ToolsGroupComponent")) {
				component = new ToolsGroupComponent(this, null);
				navigationStackPanel.add(component);
				GroupComponent gcomponent = (GroupComponent)component;
				gcomponent.setIndex(navigationStackPanel.getWidgetCount()-1);
				navigationStackPanel.setStackText(gcomponent.getIndex(), getComponentTitle(handle));
			}

			else if (handle.equals("AccountsComponent")) {
				GroupComponent accountsGroupComponent = (GroupComponent)getComponent("AccountsGroupComponent");
				component = accountsGroupComponent.getMemberComponent(handle);
			}
			else if (handle.equals("ConfigurationComponent")) {
				GroupComponent configurationGroupComponent = (GroupComponent)getComponent("ConfigurationGroupComponent");
				component = configurationGroupComponent.getMemberComponent(handle);
			}
			else if (handle.equals("CatalogComponent")) {
				GroupComponent catalogGroupComponent = (GroupComponent)getComponent("CatalogGroupComponent");
				component = catalogGroupComponent.getMemberComponent(handle);
			}
			else if (handle.equals("LocalizationComponent")) {
				GroupComponent localizationGroupComponent = (GroupComponent)getComponent("LocalizationGroupComponent");
				component = localizationGroupComponent.getMemberComponent(handle);
			}
			else if (handle.equals("LocationComponent")) {
				GroupComponent locationGroupComponent = (GroupComponent)getComponent("LocationGroupComponent");
				component = locationGroupComponent.getMemberComponent(handle);
			}
			else if (handle.equals("ModuleComponent")) {
				GroupComponent moduleGroupComponent = (GroupComponent)getComponent("ModuleGroupComponent");
				component = moduleGroupComponent.getMemberComponent(handle);
			}
			else if (handle.equals("ReportsComponent")) {
				GroupComponent reportsGroupComponent = (GroupComponent)getComponent("ReportsGroupComponent");
				component = reportsGroupComponent.getMemberComponent(handle);
			}
			else if (handle.equals("ToolsComponent")) {
				GroupComponent toolsGroupComponent = (GroupComponent)getComponent("ToolsGroupComponent");
				component = toolsGroupComponent.getMemberComponent(handle);
			}

			if (component == null) {
				GroupMemberComponent groupMemberComponent = (GroupMemberComponent)getComponent("AccountsComponent");
				component = groupMemberComponent.getComponent(handle);
			}

			if (component == null) {
				GroupMemberComponent groupMemberComponent = (GroupMemberComponent)getComponent("CatalogComponent");
				component = groupMemberComponent.getComponent(handle);
			}

			if (component == null) {
				GroupMemberComponent groupMemberComponent = (GroupMemberComponent)getComponent("LocalizationComponent");
				component = groupMemberComponent.getComponent(handle);
			}

			if (component == null) {
				GroupMemberComponent groupMemberComponent = (GroupMemberComponent)getComponent("LocationComponent");
				component = groupMemberComponent.getComponent(handle);
			}

			if (component == null) {
				GroupMemberComponent groupMemberComponent = (GroupMemberComponent)getComponent("ReportsComponent");
				component = groupMemberComponent.getComponent(handle);
			}

			if (component == null) {
				GroupMemberComponent groupMemberComponent = (GroupMemberComponent)getComponent("ToolsComponent");
				component = groupMemberComponent.getComponent(handle);
			}

			if (component != null) {
				simpleComponent = (AdminSimpleComponent)component;
				componentHash.put(handle, simpleComponent);
				component.setVisible(false);
			}
		}
		return simpleComponent;
	}

	public GWTCommerceConstants getAdminConstants() {
		return adminConstants;
	}

	public String getStoreNameAddress() {
		return storeNameAddress;
	}

	public NavigationStackPanel getNavigationStackPanel() {
		return navigationStackPanel;
	}

	public void additionalRequestParameters(CommonSimpleComponent component)
	{
		component.addRequestParameter(new RequestParameter("accountId", accountComponent.getAccountModel().getId()));
		component.addRequestParameter(new RequestParameter("languageId", languagesAdministrationComponent.getLanguageModel().getId()));

		String session = accountComponent.getAccountModel().getSession();
		if (session != null && session.length() > 0)
			component.addRequestParameter(new RequestParameter("accountSession", session));
	}
}
